import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Button,
    FlatList,
    Image,
    SectionList,
    TouchableOpacity
} from 'react-native';

var getUrl = "http://project.lanou3g.com/projects/bj/reactnative/login.php?username=lanou&password=123";
var postUrl = "http://project.lanou3g.com/projects/bj/reactnative/login.php";
export default class NetDemo extends Component {

    getRequest(url){
        /*网络请求的配置*/
        var opts = {
            method:"GET"
        }
        fetch(url,opts)
            .then((response) => {
                return response.text();
            })
            .then((responseText) => {
                alert(responseText);
            })
            .catch((error) =>{
                alert(error);
            })
    }
    postRequest(url){
        let formData = new FormData();
        formData.append("username","SuperBigLw");
        formData.append("password","123456");
        var opts = {
            method:"POST",
            body:formData
        }
        fetch(url,opts)
            .then((response) => {
                return response.text();
            })
            .then((responseText) => {
                alert(responseText);
            })
            .catch((error) => {
                alert(error)
            })
    }

    render(){

        return(
            <View style={styles.container}>
                <TouchableOpacity onPress={this.getRequest.bind(this,getUrl)}>
                    <View style={styles.btn}>
                        <Text>GET</Text>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity onPress={this.postRequest.bind(this,postUrl)}>
                    <View style={styles.btn}>
                        <Text>POST</Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container:{
        backgroundColor:"cyan",
        flexDirection:"row",
        justifyContent:"space-around",
        alignItems:"center",
        flex:1
    },
    btn:{
        width:60,
        height:30,
        borderWidth:1,
        borderRadius:3,
        borderColor:"black",
        backgroundColor:"yellow",
        justifyContent:"center",
        alignItems:"center"

    }
});